/*
 * File:    RenderEngine.java
 * Created: 04-Feb-2009
 * Version: $Id$
 *
 * COPYRIGHT (C) 2009, Bitgate Mobile, LLC.  All Rights Reserved.
 *
 * software@bitgatemobile.com
 */

package com.webplasm.render.engine;

import static com.bitgate.util.debug.Debug.debug;

import com.webplasm.context.RenderContext;
import com.webplasm.render.tags.WrappedNode;

/**
 * This class takes an XML document in plain text, parses it using the <code>DocumentParser</code> class, then
 * walks the document tree by wrapping the nodes with <code>WrappedNode</code> objects for rendering.  This class
 * is the entry point used to render a document in the Webplasm system.
 * 
 * @author kenji
 * @since Webplasm 2.0
 */
public class RenderEngine {
	/**
	 * Prepares a document for rendering by wrapping its contents after XML parsing with <code>WrappedNode</code>
	 * objects.  It then processes the document through the tree, and returns the resulting document after rendering
	 * as a <code>StringBuffer</code> object.
	 * 
	 * @param data <code>byte[]</code> containing the well-formed XML document to parse.
	 * @return <code>StringBuffer</code> containing the resulting document after rendering.
	 */
	public StringBuffer render(byte[] data, RenderContext renderContext) throws RenderException {
		DocumentParser dParser = null;
		
		try {
			debug("Data: " + new String(data));
			dParser = new DocumentParser(data);
		} catch(Exception ex) {
			debug("Unable to parse document.");
			return new StringBuffer("Your document failed to parse properly.");
		}
		
		WrappedNode wrappedNode = new WrappedNode(dParser.getRootNode());
		
		return wrappedNode.render(renderContext);
	}
}